package com.thinkmore.framework.utils;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
public abstract class UUIDUtil {
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}

	/**
	 * 获得指定数目的UUID
	 * 
	 * @param number
	 *            int 需要获得的UUID数量
	 * @return String[] UUID数组
	 */
	public static String[] getUUID(int number) {
		if (number < 1) {
			return null;
		}
		String[] ss = new String[number];
		for (int i = 0; i < number; i++) {
			ss[i] = uuid();
		}
		return ss;
	}

	public static String base64Uuid() {
		UUID uuid = UUID.randomUUID();
		return base64Uuid(uuid);
	}

	protected static String base64Uuid(UUID uuid) {
		ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
		bb.putLong(uuid.getMostSignificantBits());
		bb.putLong(uuid.getLeastSignificantBits());
		return Base64.encodeBase64URLSafeString(bb.array());
	}

	public static String encodeBase64Uuid(String uuidString) {
		UUID uuid = UUID.fromString(uuidString);
		return base64Uuid(uuid);
	}

	public static String decodeBase64Uuid(String compressedUuid) {
		byte[] byUuid = Base64.decodeBase64(compressedUuid);
		ByteBuffer bb = ByteBuffer.wrap(byUuid);
		UUID uuid = new UUID(bb.getLong(), bb.getLong());
		return uuid.toString();
	}

	public static void main(String[] args) {
		System.out.println(uuid());
	}
}
